/**
* @authors Duong NGUYEN <darl2ng@gmail.com>, Anh NGO <ngocanh.ngo@gmail.com>
* @version 0.9
* Copyright 2010
* 
* The MIT License
* Copyright (c) 2010 <Duong NGUYEN <darl2ng@gmail.com>, Anh NGO <ngocanh.ngo@gmail.com>
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package  {
	
	import com.ng.ResHelp.c_Builder;
	import com.ng.ResHelp.c_ResHelper;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.utils.getDefinitionByName;

	public class c_ResHelper2 extends c_ResHelper	{
		
		static private var __OverrideInstance:c_ResHelper2;
		
		public var MaxID: Number = 0;
		
		static public function GetInstance():c_ResHelper2 {
			if(__OverrideInstance == null)
				__OverrideInstance = new c_ResHelper2();
			return __OverrideInstance;
		}				
		public function c_ResHelper2(){
			super();
		}			
		public function getResObjectById(frame_id:String):c_ResObject {
			var o:c_ResObject = new c_ResObject();
			setFrameById(o,frame_id);
			return o;
		}
		public function addItem(item:c_Item,fr:Object):void {
			var cl:Class = this.getClassById(item.xml.attribute('class'));
			if (cl != null) {																				
				c.d2('adding: ' + item.xml.attribute('id'));
				var obj :c_ResObject = new c_ResObject();
				obj.Init(item.xml, new cl() as Sprite);
				obj.name = obj.Content.name;
				fr.addChild(obj);									
			} else {				
				c.e("ClassNotExist["+item.attribute('class')+"]");
			}
		}
		/**
		 * won't throw error exception for controler class since we do not need to use them in editor
		 * @param	id
		 * @return
		 */
		protected override function registerControlerClasses():void {
			for each (var item:XML in __XML.Classes.Class) {
				var id:String = item.attribute("id");
				var cl:Class;
				try {
					cl = getDefinitionByName(id) as Class;
				} catch (e:ReferenceError) {
					//throw new Error("ClassNotExist[" + id +"]");
				}
				if (cl != null) {
					__ResLoader.classes[id] = cl;
				}
			}
		}
		/**
		 * add xml to c_ResObject
		 * @param	o
		 * @param	frame_id
		 */
		public override function setFrameById(o:Object,frame_id:String):void {
			c.d2('setFrameById '+frame_id);
			//this should be unique but people are crazy sometimes :-)
			for each (var frame:XML in __XML.Presentation.Frame.(@id == frame_id)) {
				if (frame.length()>1) c.e("frame id not unique");
				fetchChildren(frame.Element, o as Sprite);
				o.name = frame_id;
				c_Builder.setAttributes(frame, o, __MovieClipClassReference);
				(o as c_ResObject).xml = frame;				
			}			
		}
		/**
		 * make use of c_ResObject (with same name as its content)
		 * remove controler
		 * 
		 * @param	elements
		 * @param	fr
		 * @return
		 */
		protected override function fetchChildren(elements:XMLList, fr:Object):void{
			c.d2('fetchChildren: ' + elements);
			for each (var item:XML in elements) {				
				var cl:Class = this.getClassById(item.attribute('class'));
				if (cl != null) {																				
					c.d1('adding: ' + item.attribute("id")+' class :'+cl);
					var obj :c_ResObject = new c_ResObject();
					obj.Init(item, new cl() as DisplayObject);
					
					obj.name = obj.Content.name;
					fr.addChild(obj);
					//parse and add children recursively if need to					
					if (item.Element.length() > 0) {
						this.fetchChildren(item.Element, obj);
					}
				} else {
					c.e("ClassNotExist["+item.attribute('class')+"]");
				}
			}
		}			
	}
}